<!DOCTYPE html>
<html lang="en-US">
  
<!-- Mirrored from heavy_code_industry.gitee.io/code_heavy_industry/pro018-MySQL-high/lecture/chapter05/index.html by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 10 May 2022 05:13:08 GMT -->
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>代码重工</title>
    <meta name="generator" content="VuePress 1.9.5">
    
    <meta name="description" content="代码重工">
    
    <link rel="preload" href="../../../assets/css/0.styles.f7de6701.css" as="style"><link rel="preload" href="../../../assets/js/app.972bd0f0.js" as="script"><link rel="preload" href="../../../assets/js/4.904e76b5.js" as="script"><link rel="preload" href="../../../assets/js/18.9167733d.js" as="script"><link rel="prefetch" href="../../../assets/js/1.d66e04b2.js"><link rel="prefetch" href="../../../assets/js/10.7093d550.js"><link rel="prefetch" href="../../../assets/js/100.384331b2.js"><link rel="prefetch" href="../../../assets/js/101.582f78e7.js"><link rel="prefetch" href="../../../assets/js/102.17f2c0ec.js"><link rel="prefetch" href="../../../assets/js/103.a530857d.js"><link rel="prefetch" href="../../../assets/js/104.62821ce1.js"><link rel="prefetch" href="../../../assets/js/105.11cdcc82.js"><link rel="prefetch" href="../../../assets/js/106.d57c1e75.js"><link rel="prefetch" href="../../../assets/js/107.7ebeb306.js"><link rel="prefetch" href="../../../assets/js/108.c067b47b.js"><link rel="prefetch" href="../../../assets/js/109.9495d2ca.js"><link rel="prefetch" href="../../../assets/js/11.acc2935e.js"><link rel="prefetch" href="../../../assets/js/110.7f58dbd3.js"><link rel="prefetch" href="../../../assets/js/111.f78239ff.js"><link rel="prefetch" href="../../../assets/js/112.8eb72445.js"><link rel="prefetch" href="../../../assets/js/113.3f5aff71.js"><link rel="prefetch" href="../../../assets/js/114.9f65cf57.js"><link rel="prefetch" href="../../../assets/js/115.dcaea004.js"><link rel="prefetch" href="../../../assets/js/116.726c5aac.js"><link rel="prefetch" href="../../../assets/js/117.54d6c26a.js"><link rel="prefetch" href="../../../assets/js/118.3f4e0bda.js"><link rel="prefetch" href="../../../assets/js/119.95ed5dc6.js"><link rel="prefetch" href="../../../assets/js/12.a5354f50.js"><link rel="prefetch" href="../../../assets/js/120.4fd08fac.js"><link rel="prefetch" href="../../../assets/js/121.f4aadbe5.js"><link rel="prefetch" href="../../../assets/js/122.d588a1e0.js"><link rel="prefetch" href="../../../assets/js/123.781e7192.js"><link rel="prefetch" href="../../../assets/js/124.ed312291.js"><link rel="prefetch" href="../../../assets/js/125.bceffbb9.js"><link rel="prefetch" href="../../../assets/js/126.51bf552f.js"><link rel="prefetch" href="../../../assets/js/127.4800687b.js"><link rel="prefetch" href="../../../assets/js/128.fcdd6f2a.js"><link rel="prefetch" href="../../../assets/js/129.2a25d78a.js"><link rel="prefetch" href="../../../assets/js/13.88a0c565.js"><link rel="prefetch" href="../../../assets/js/130.dcfe7623.js"><link rel="prefetch" href="../../../assets/js/131.c29e7c45.js"><link rel="prefetch" href="../../../assets/js/132.9a412a4b.js"><link rel="prefetch" href="../../../assets/js/133.615218d5.js"><link rel="prefetch" href="../../../assets/js/134.2168ca5a.js"><link rel="prefetch" href="../../../assets/js/135.a5e262ff.js"><link rel="prefetch" href="../../../assets/js/136.7dba5235.js"><link rel="prefetch" href="../../../assets/js/137.445576e7.js"><link rel="prefetch" href="../../../assets/js/138.577b3f66.js"><link rel="prefetch" href="../../../assets/js/139.b646f1ac.js"><link rel="prefetch" href="../../../assets/js/14.36391765.js"><link rel="prefetch" href="../../../assets/js/140.52abf81e.js"><link rel="prefetch" href="../../../assets/js/141.807c626b.js"><link rel="prefetch" href="../../../assets/js/142.adf9e127.js"><link rel="prefetch" href="../../../assets/js/143.9773d979.js"><link rel="prefetch" href="../../../assets/js/144.5658ae76.js"><link rel="prefetch" href="../../../assets/js/145.2d2fb13b.js"><link rel="prefetch" href="../../../assets/js/146.0e322019.js"><link rel="prefetch" href="../../../assets/js/147.50b34ac1.js"><link rel="prefetch" href="../../../assets/js/148.463aab7c.js"><link rel="prefetch" href="../../../assets/js/149.9bd7c539.js"><link rel="prefetch" href="../../../assets/js/15.4a8e9d28.js"><link rel="prefetch" href="../../../assets/js/150.9449ca28.js"><link rel="prefetch" href="../../../assets/js/151.1961c6db.js"><link rel="prefetch" href="../../../assets/js/152.18709656.js"><link rel="prefetch" href="../../../assets/js/153.a4bf78f6.js"><link rel="prefetch" href="../../../assets/js/154.6c90b1d5.js"><link rel="prefetch" href="../../../assets/js/155.024247e2.js"><link rel="prefetch" href="../../../assets/js/156.76281b57.js"><link rel="prefetch" href="../../../assets/js/157.a7c02212.js"><link rel="prefetch" href="../../../assets/js/158.0d00acef.js"><link rel="prefetch" href="../../../assets/js/159.9180c937.js"><link rel="prefetch" href="../../../assets/js/16.87793b5d.js"><link rel="prefetch" href="../../../assets/js/160.d7a98288.js"><link rel="prefetch" href="../../../assets/js/161.389ddf84.js"><link rel="prefetch" href="../../../assets/js/162.ab22d7dc.js"><link rel="prefetch" href="../../../assets/js/163.087257c6.js"><link rel="prefetch" href="../../../assets/js/164.addfc5af.js"><link rel="prefetch" href="../../../assets/js/165.f90e6b2a.js"><link rel="prefetch" href="../../../assets/js/166.fa06e4b6.js"><link rel="prefetch" href="../../../assets/js/167.a00e722b.js"><link rel="prefetch" href="../../../assets/js/168.e3855223.js"><link rel="prefetch" href="../../../assets/js/169.e0c63a4e.js"><link rel="prefetch" href="../../../assets/js/17.dec86310.js"><link rel="prefetch" href="../../../assets/js/170.7c5835f4.js"><link rel="prefetch" href="../../../assets/js/171.7d9ae110.js"><link rel="prefetch" href="../../../assets/js/172.4da5caea.js"><link rel="prefetch" href="../../../assets/js/173.f8ac6155.js"><link rel="prefetch" href="../../../assets/js/174.3b831765.js"><link rel="prefetch" href="../../../assets/js/175.01782f3d.js"><link rel="prefetch" href="../../../assets/js/176.6eab6644.js"><link rel="prefetch" href="../../../assets/js/177.86339767.js"><link rel="prefetch" href="../../../assets/js/178.c437c804.js"><link rel="prefetch" href="../../../assets/js/179.927227c9.js"><link rel="prefetch" href="../../../assets/js/180.caa2c660.js"><link rel="prefetch" href="../../../assets/js/181.c186b8ac.js"><link rel="prefetch" href="../../../assets/js/182.0475844f.js"><link rel="prefetch" href="../../../assets/js/183.2c5950c8.js"><link rel="prefetch" href="../../../assets/js/184.82b336f6.js"><link rel="prefetch" href="../../../assets/js/185.e2d4a46e.js"><link rel="prefetch" href="../../../assets/js/186.99a3de8a.js"><link rel="prefetch" href="../../../assets/js/187.d557500c.js"><link rel="prefetch" href="../../../assets/js/188.e81bd251.js"><link rel="prefetch" href="../../../assets/js/189.d07d368a.js"><link rel="prefetch" href="../../../assets/js/19.017b4ad0.js"><link rel="prefetch" href="../../../assets/js/190.173606ea.js"><link rel="prefetch" href="../../../assets/js/191.c5ac0c71.js"><link rel="prefetch" href="../../../assets/js/192.127d6d93.js"><link rel="prefetch" href="../../../assets/js/193.2f9011c9.js"><link rel="prefetch" href="../../../assets/js/194.14f534a3.js"><link rel="prefetch" href="../../../assets/js/195.65939036.js"><link rel="prefetch" href="../../../assets/js/196.6e3cf025.js"><link rel="prefetch" href="../../../assets/js/197.4d18b1d7.js"><link rel="prefetch" href="../../../assets/js/198.c2e521c3.js"><link rel="prefetch" href="../../../assets/js/199.d72c63dd.js"><link rel="prefetch" href="../../../assets/js/20.6aa686d0.js"><link rel="prefetch" href="../../../assets/js/200.56d7dbd3.js"><link rel="prefetch" href="../../../assets/js/201.c72a395d.js"><link rel="prefetch" href="../../../assets/js/202.cb32f7af.js"><link rel="prefetch" href="../../../assets/js/203.285e6b92.js"><link rel="prefetch" href="../../../assets/js/204.ead50027.js"><link rel="prefetch" href="../../../assets/js/205.190c1baf.js"><link rel="prefetch" href="../../../assets/js/206.4dab4ba0.js"><link rel="prefetch" href="../../../assets/js/207.f11eee48.js"><link rel="prefetch" href="../../../assets/js/208.e2f8ca82.js"><link rel="prefetch" href="../../../assets/js/209.e7e74aff.js"><link rel="prefetch" href="../../../assets/js/21.f7d66fe8.js"><link rel="prefetch" href="../../../assets/js/210.69c4a0ed.js"><link rel="prefetch" href="../../../assets/js/211.a1ab5062.js"><link rel="prefetch" href="../../../assets/js/212.6d0473bd.js"><link rel="prefetch" href="../../../assets/js/213.bd120419.js"><link rel="prefetch" href="../../../assets/js/214.c7d4362c.js"><link rel="prefetch" href="../../../assets/js/215.8adc34dc.js"><link rel="prefetch" href="../../../assets/js/216.abf8ff23.js"><link rel="prefetch" href="../../../assets/js/217.10f9fc46.js"><link rel="prefetch" href="../../../assets/js/218.52e31fec.js"><link rel="prefetch" href="../../../assets/js/219.ea0551af.js"><link rel="prefetch" href="../../../assets/js/22.b1de9b18.js"><link rel="prefetch" href="../../../assets/js/220.6be520c8.js"><link rel="prefetch" href="../../../assets/js/221.c0d93576.js"><link rel="prefetch" href="../../../assets/js/222.1bb0eaff.js"><link rel="prefetch" href="../../../assets/js/223.8492b065.js"><link rel="prefetch" href="../../../assets/js/224.5897434e.js"><link rel="prefetch" href="../../../assets/js/225.51902428.js"><link rel="prefetch" href="../../../assets/js/226.9da70eee.js"><link rel="prefetch" href="../../../assets/js/227.9e113839.js"><link rel="prefetch" href="../../../assets/js/228.d689b27d.js"><link rel="prefetch" href="../../../assets/js/229.28f74090.js"><link rel="prefetch" href="../../../assets/js/23.e3b58d3d.js"><link rel="prefetch" href="../../../assets/js/230.465a35a0.js"><link rel="prefetch" href="../../../assets/js/231.219aeb63.js"><link rel="prefetch" href="../../../assets/js/232.84035c50.js"><link rel="prefetch" href="../../../assets/js/233.ef27424d.js"><link rel="prefetch" href="../../../assets/js/234.5dcf1349.js"><link rel="prefetch" href="../../../assets/js/235.bcc0c537.js"><link rel="prefetch" href="../../../assets/js/236.9bb64465.js"><link rel="prefetch" href="../../../assets/js/237.f3300514.js"><link rel="prefetch" href="../../../assets/js/238.ca7af9ac.js"><link rel="prefetch" href="../../../assets/js/239.fdfe2127.js"><link rel="prefetch" href="../../../assets/js/24.2e010582.js"><link rel="prefetch" href="../../../assets/js/240.8a4488af.js"><link rel="prefetch" href="../../../assets/js/241.9987dfab.js"><link rel="prefetch" href="../../../assets/js/242.819ed984.js"><link rel="prefetch" href="../../../assets/js/243.bf129ecd.js"><link rel="prefetch" href="../../../assets/js/244.030b4b49.js"><link rel="prefetch" href="../../../assets/js/245.7663f620.js"><link rel="prefetch" href="../../../assets/js/246.ed2b2fc0.js"><link rel="prefetch" href="../../../assets/js/247.54dd84f1.js"><link rel="prefetch" href="../../../assets/js/248.a1c69038.js"><link rel="prefetch" href="../../../assets/js/249.47dd7b3b.js"><link rel="prefetch" href="../../../assets/js/25.a5f2f737.js"><link rel="prefetch" href="../../../assets/js/250.ec8017a5.js"><link rel="prefetch" href="../../../assets/js/251.89ab0cf7.js"><link rel="prefetch" href="../../../assets/js/252.5ec1eea4.js"><link rel="prefetch" href="../../../assets/js/253.31a37280.js"><link rel="prefetch" href="../../../assets/js/254.fcfabb55.js"><link rel="prefetch" href="../../../assets/js/255.286f4c17.js"><link rel="prefetch" href="../../../assets/js/256.ce77595b.js"><link rel="prefetch" href="../../../assets/js/257.cb3677c0.js"><link rel="prefetch" href="../../../assets/js/258.ca18e0e9.js"><link rel="prefetch" href="../../../assets/js/259.65dc8492.js"><link rel="prefetch" href="../../../assets/js/26.cce346fc.js"><link rel="prefetch" href="../../../assets/js/260.475ddaba.js"><link rel="prefetch" href="../../../assets/js/261.643b784a.js"><link rel="prefetch" href="../../../assets/js/262.e5092c67.js"><link rel="prefetch" href="../../../assets/js/263.5e85d32e.js"><link rel="prefetch" href="../../../assets/js/264.6ff4b789.js"><link rel="prefetch" href="../../../assets/js/265.fee7416a.js"><link rel="prefetch" href="../../../assets/js/266.8d8a5e44.js"><link rel="prefetch" href="../../../assets/js/267.8c62a655.js"><link rel="prefetch" href="../../../assets/js/268.762073ff.js"><link rel="prefetch" href="../../../assets/js/269.ffe69921.js"><link rel="prefetch" href="../../../assets/js/27.22790cda.js"><link rel="prefetch" href="../../../assets/js/270.d5bf783f.js"><link rel="prefetch" href="../../../assets/js/271.83fd2f70.js"><link rel="prefetch" href="../../../assets/js/272.6de67fa9.js"><link rel="prefetch" href="../../../assets/js/273.628e1b77.js"><link rel="prefetch" href="../../../assets/js/274.7f0d82c6.js"><link rel="prefetch" href="../../../assets/js/275.d4478098.js"><link rel="prefetch" href="../../../assets/js/276.56649e5b.js"><link rel="prefetch" href="../../../assets/js/277.05b881f2.js"><link rel="prefetch" href="../../../assets/js/278.c079de28.js"><link rel="prefetch" href="../../../assets/js/279.58101972.js"><link rel="prefetch" href="../../../assets/js/28.ac0ba3fa.js"><link rel="prefetch" href="../../../assets/js/280.5e32c770.js"><link rel="prefetch" href="../../../assets/js/281.59fb6cf4.js"><link rel="prefetch" href="../../../assets/js/282.f22da512.js"><link rel="prefetch" href="../../../assets/js/283.db103d63.js"><link rel="prefetch" href="../../../assets/js/284.a373c7e0.js"><link rel="prefetch" href="../../../assets/js/285.55c6f720.js"><link rel="prefetch" href="../../../assets/js/286.f924c38c.js"><link rel="prefetch" href="../../../assets/js/287.52a35525.js"><link rel="prefetch" href="../../../assets/js/288.dcc32270.js"><link rel="prefetch" href="../../../assets/js/289.835ad2b5.js"><link rel="prefetch" href="../../../assets/js/29.6e1dc238.js"><link rel="prefetch" href="../../../assets/js/290.749e38a9.js"><link rel="prefetch" href="../../../assets/js/291.90916822.js"><link rel="prefetch" href="../../../assets/js/292.b453b275.js"><link rel="prefetch" href="../../../assets/js/293.11ad0754.js"><link rel="prefetch" href="../../../assets/js/294.dddc45b9.js"><link rel="prefetch" href="../../../assets/js/295.74ecca38.js"><link rel="prefetch" href="../../../assets/js/296.2061efa6.js"><link rel="prefetch" href="../../../assets/js/297.b838fcd5.js"><link rel="prefetch" href="../../../assets/js/298.8c08be1f.js"><link rel="prefetch" href="../../../assets/js/299.5a99490b.js"><link rel="prefetch" href="../../../assets/js/3.9ca9fb75.js"><link rel="prefetch" href="../../../assets/js/30.64f9cad0.js"><link rel="prefetch" href="../../../assets/js/300.a5010762.js"><link rel="prefetch" href="../../../assets/js/301.a2f8795a.js"><link rel="prefetch" href="../../../assets/js/302.129bdff3.js"><link rel="prefetch" href="../../../assets/js/303.57268b2c.js"><link rel="prefetch" href="../../../assets/js/304.5556b94f.js"><link rel="prefetch" href="../../../assets/js/305.72bbf9b8.js"><link rel="prefetch" href="../../../assets/js/306.821163a7.js"><link rel="prefetch" href="../../../assets/js/307.b85ceac7.js"><link rel="prefetch" href="../../../assets/js/308.7f794216.js"><link rel="prefetch" href="../../../assets/js/309.212b7a1d.js"><link rel="prefetch" href="../../../assets/js/31.386297cc.js"><link rel="prefetch" href="../../../assets/js/310.089b4a08.js"><link rel="prefetch" href="../../../assets/js/311.c7f6aabd.js"><link rel="prefetch" href="../../../assets/js/312.810271c0.js"><link rel="prefetch" href="../../../assets/js/313.030424f0.js"><link rel="prefetch" href="../../../assets/js/314.a95451b6.js"><link rel="prefetch" href="../../../assets/js/315.f883b88e.js"><link rel="prefetch" href="../../../assets/js/316.676ed945.js"><link rel="prefetch" href="../../../assets/js/317.0dcbeaef.js"><link rel="prefetch" href="../../../assets/js/318.bd172af6.js"><link rel="prefetch" href="../../../assets/js/319.e2d01ca1.js"><link rel="prefetch" href="../../../assets/js/32.8927ba28.js"><link rel="prefetch" href="../../../assets/js/320.09b01482.js"><link rel="prefetch" href="../../../assets/js/321.3b70dce2.js"><link rel="prefetch" href="../../../assets/js/322.5f7046ad.js"><link rel="prefetch" href="../../../assets/js/323.ffd56783.js"><link rel="prefetch" href="../../../assets/js/324.cfe0b188.js"><link rel="prefetch" href="../../../assets/js/325.cc2f1112.js"><link rel="prefetch" href="../../../assets/js/326.635a861b.js"><link rel="prefetch" href="../../../assets/js/327.94eacc0e.js"><link rel="prefetch" href="../../../assets/js/328.7d4790fb.js"><link rel="prefetch" href="../../../assets/js/329.38f7ef56.js"><link rel="prefetch" href="../../../assets/js/33.bd1f28e6.js"><link rel="prefetch" href="../../../assets/js/330.269edfe2.js"><link rel="prefetch" href="../../../assets/js/331.21623a36.js"><link rel="prefetch" href="../../../assets/js/332.1f1c659c.js"><link rel="prefetch" href="../../../assets/js/333.fe34384d.js"><link rel="prefetch" href="../../../assets/js/334.34a514a7.js"><link rel="prefetch" href="../../../assets/js/335.9272b0cc.js"><link rel="prefetch" href="../../../assets/js/336.796dcaf9.js"><link rel="prefetch" href="../../../assets/js/337.bfd29430.js"><link rel="prefetch" href="../../../assets/js/338.fdb020fd.js"><link rel="prefetch" href="../../../assets/js/339.d7f19890.js"><link rel="prefetch" href="../../../assets/js/34.36a2460a.js"><link rel="prefetch" href="../../../assets/js/340.b22b3f52.js"><link rel="prefetch" href="../../../assets/js/341.47e1d3f4.js"><link rel="prefetch" href="../../../assets/js/342.38ca17d1.js"><link rel="prefetch" href="../../../assets/js/343.2e7874e5.js"><link rel="prefetch" href="../../../assets/js/344.6697aea1.js"><link rel="prefetch" href="../../../assets/js/345.ad3be23a.js"><link rel="prefetch" href="../../../assets/js/346.d40fa749.js"><link rel="prefetch" href="../../../assets/js/347.881934aa.js"><link rel="prefetch" href="../../../assets/js/348.2e8d29b9.js"><link rel="prefetch" href="../../../assets/js/349.87f51cb4.js"><link rel="prefetch" href="../../../assets/js/35.80f36815.js"><link rel="prefetch" href="../../../assets/js/350.06081b68.js"><link rel="prefetch" href="../../../assets/js/351.f883c9ae.js"><link rel="prefetch" href="../../../assets/js/352.8fcd0728.js"><link rel="prefetch" href="../../../assets/js/353.2dd787b0.js"><link rel="prefetch" href="../../../assets/js/354.689db664.js"><link rel="prefetch" href="../../../assets/js/355.b9a083ea.js"><link rel="prefetch" href="../../../assets/js/356.3eaa4bf3.js"><link rel="prefetch" href="../../../assets/js/357.2f05ac11.js"><link rel="prefetch" href="../../../assets/js/358.c6777610.js"><link rel="prefetch" href="../../../assets/js/359.2ae0c8ac.js"><link rel="prefetch" href="../../../assets/js/36.f9bba134.js"><link rel="prefetch" href="../../../assets/js/360.c9811603.js"><link rel="prefetch" href="../../../assets/js/361.5c3068d4.js"><link rel="prefetch" href="../../../assets/js/362.a80ddcc6.js"><link rel="prefetch" href="../../../assets/js/363.ccbb8d6e.js"><link rel="prefetch" href="../../../assets/js/364.8da5de40.js"><link rel="prefetch" href="../../../assets/js/365.0d9f6c8a.js"><link rel="prefetch" href="../../../assets/js/366.e410e582.js"><link rel="prefetch" href="../../../assets/js/367.f8689b17.js"><link rel="prefetch" href="../../../assets/js/368.57119763.js"><link rel="prefetch" href="../../../assets/js/369.f92c7aed.js"><link rel="prefetch" href="../../../assets/js/37.8935c848.js"><link rel="prefetch" href="../../../assets/js/370.8e896c3a.js"><link rel="prefetch" href="../../../assets/js/371.e2565759.js"><link rel="prefetch" href="../../../assets/js/372.0028b0ce.js"><link rel="prefetch" href="../../../assets/js/373.82bfe997.js"><link rel="prefetch" href="../../../assets/js/374.434f814f.js"><link rel="prefetch" href="../../../assets/js/375.8478c681.js"><link rel="prefetch" href="../../../assets/js/376.998ce2de.js"><link rel="prefetch" href="../../../assets/js/377.670e9be5.js"><link rel="prefetch" href="../../../assets/js/378.c9cfc01b.js"><link rel="prefetch" href="../../../assets/js/379.0aacc719.js"><link rel="prefetch" href="../../../assets/js/38.86fba1a7.js"><link rel="prefetch" href="../../../assets/js/380.14a91411.js"><link rel="prefetch" href="../../../assets/js/381.9e783c93.js"><link rel="prefetch" href="../../../assets/js/382.cf2ffc03.js"><link rel="prefetch" href="../../../assets/js/383.c32b0740.js"><link rel="prefetch" href="../../../assets/js/384.4280025f.js"><link rel="prefetch" href="../../../assets/js/385.a816d541.js"><link rel="prefetch" href="../../../assets/js/386.1d8fc395.js"><link rel="prefetch" href="../../../assets/js/387.bc22626f.js"><link rel="prefetch" href="../../../assets/js/388.1cf59352.js"><link rel="prefetch" href="../../../assets/js/389.a09795bf.js"><link rel="prefetch" href="../../../assets/js/39.c8c0ff8e.js"><link rel="prefetch" href="../../../assets/js/390.6a223692.js"><link rel="prefetch" href="../../../assets/js/391.733d45f0.js"><link rel="prefetch" href="../../../assets/js/392.d76fd3df.js"><link rel="prefetch" href="../../../assets/js/393.c885df90.js"><link rel="prefetch" href="../../../assets/js/394.c344d389.js"><link rel="prefetch" href="../../../assets/js/395.687cbd0a.js"><link rel="prefetch" href="../../../assets/js/396.57e0df54.js"><link rel="prefetch" href="../../../assets/js/397.1a31ad36.js"><link rel="prefetch" href="../../../assets/js/398.59535325.js"><link rel="prefetch" href="../../../assets/js/399.0a11557f.js"><link rel="prefetch" href="../../../assets/js/40.c0f7e504.js"><link rel="prefetch" href="../../../assets/js/400.7b2afabb.js"><link rel="prefetch" href="../../../assets/js/401.4b72ea6e.js"><link rel="prefetch" href="../../../assets/js/402.b94737bf.js"><link rel="prefetch" href="../../../assets/js/403.a838c189.js"><link rel="prefetch" href="../../../assets/js/404.9a221abc.js"><link rel="prefetch" href="../../../assets/js/405.f8f3b954.js"><link rel="prefetch" href="../../../assets/js/406.2de8752c.js"><link rel="prefetch" href="../../../assets/js/407.eb35a188.js"><link rel="prefetch" href="../../../assets/js/408.47acaedf.js"><link rel="prefetch" href="../../../assets/js/409.aa2afaca.js"><link rel="prefetch" href="../../../assets/js/41.e7e858c4.js"><link rel="prefetch" href="../../../assets/js/410.722e5009.js"><link rel="prefetch" href="../../../assets/js/411.2dbe321f.js"><link rel="prefetch" href="../../../assets/js/412.39ed2d1e.js"><link rel="prefetch" href="../../../assets/js/413.a088f973.js"><link rel="prefetch" href="../../../assets/js/414.87f8d239.js"><link rel="prefetch" href="../../../assets/js/415.f90a1394.js"><link rel="prefetch" href="../../../assets/js/416.ea4ba38d.js"><link rel="prefetch" href="../../../assets/js/417.8a1c9676.js"><link rel="prefetch" href="../../../assets/js/418.79315748.js"><link rel="prefetch" href="../../../assets/js/419.1e3ddf12.js"><link rel="prefetch" href="../../../assets/js/42.8202bb44.js"><link rel="prefetch" href="../../../assets/js/420.b6d9e0cb.js"><link rel="prefetch" href="../../../assets/js/421.0c74ef64.js"><link rel="prefetch" href="../../../assets/js/422.94c20620.js"><link rel="prefetch" href="../../../assets/js/423.fa0d6946.js"><link rel="prefetch" href="../../../assets/js/424.9036ac4f.js"><link rel="prefetch" href="../../../assets/js/425.8d8e82da.js"><link rel="prefetch" href="../../../assets/js/426.03b7da89.js"><link rel="prefetch" href="../../../assets/js/427.e3c8c690.js"><link rel="prefetch" href="../../../assets/js/428.96fdfdce.js"><link rel="prefetch" href="../../../assets/js/429.a8863be5.js"><link rel="prefetch" href="../../../assets/js/43.55e30ded.js"><link rel="prefetch" href="../../../assets/js/430.305aa090.js"><link rel="prefetch" href="../../../assets/js/431.e02d3725.js"><link rel="prefetch" href="../../../assets/js/432.83fa148f.js"><link rel="prefetch" href="../../../assets/js/433.1f01715d.js"><link rel="prefetch" href="../../../assets/js/434.f3354ec6.js"><link rel="prefetch" href="../../../assets/js/435.245644ed.js"><link rel="prefetch" href="../../../assets/js/436.3c933a14.js"><link rel="prefetch" href="../../../assets/js/437.b62325fb.js"><link rel="prefetch" href="../../../assets/js/438.8e122783.js"><link rel="prefetch" href="../../../assets/js/439.e95fa7e6.js"><link rel="prefetch" href="../../../assets/js/44.e5c90e7a.js"><link rel="prefetch" href="../../../assets/js/440.5c168090.js"><link rel="prefetch" href="../../../assets/js/441.f8835ac7.js"><link rel="prefetch" href="../../../assets/js/442.cf1de5dc.js"><link rel="prefetch" href="../../../assets/js/443.50ec8b3c.js"><link rel="prefetch" href="../../../assets/js/444.1600e74f.js"><link rel="prefetch" href="../../../assets/js/445.795f336d.js"><link rel="prefetch" href="../../../assets/js/446.1fb71b0f.js"><link rel="prefetch" href="../../../assets/js/447.0136df19.js"><link rel="prefetch" href="../../../assets/js/448.abf11aa4.js"><link rel="prefetch" href="../../../assets/js/449.4f99bee8.js"><link rel="prefetch" href="../../../assets/js/45.89368cde.js"><link rel="prefetch" href="../../../assets/js/450.55da381a.js"><link rel="prefetch" href="../../../assets/js/451.97a68e8d.js"><link rel="prefetch" href="../../../assets/js/452.9dd8c55e.js"><link rel="prefetch" href="../../../assets/js/453.3167da0d.js"><link rel="prefetch" href="../../../assets/js/454.18fad993.js"><link rel="prefetch" href="../../../assets/js/455.24cbebda.js"><link rel="prefetch" href="../../../assets/js/456.a2f19b40.js"><link rel="prefetch" href="../../../assets/js/457.27f4fb5a.js"><link rel="prefetch" href="../../../assets/js/458.29941084.js"><link rel="prefetch" href="../../../assets/js/459.b0cc6f89.js"><link rel="prefetch" href="../../../assets/js/46.1f553681.js"><link rel="prefetch" href="../../../assets/js/460.86ef4965.js"><link rel="prefetch" href="../../../assets/js/461.c0b0f695.js"><link rel="prefetch" href="../../../assets/js/462.8b86c3a9.js"><link rel="prefetch" href="../../../assets/js/463.79aadbb4.js"><link rel="prefetch" href="../../../assets/js/464.9735f749.js"><link rel="prefetch" href="../../../assets/js/465.4bdede2b.js"><link rel="prefetch" href="../../../assets/js/466.ef630311.js"><link rel="prefetch" href="../../../assets/js/467.8664e027.js"><link rel="prefetch" href="../../../assets/js/468.89b3d4ff.js"><link rel="prefetch" href="../../../assets/js/469.74e0af2c.js"><link rel="prefetch" href="../../../assets/js/47.5f78034c.js"><link rel="prefetch" href="../../../assets/js/470.9643f84e.js"><link rel="prefetch" href="../../../assets/js/471.e3a1e16a.js"><link rel="prefetch" href="../../../assets/js/472.116791e2.js"><link rel="prefetch" href="../../../assets/js/473.3bb6cb3d.js"><link rel="prefetch" href="../../../assets/js/474.1a7a0724.js"><link rel="prefetch" href="../../../assets/js/475.76c29d71.js"><link rel="prefetch" href="../../../assets/js/476.fe1fd93a.js"><link rel="prefetch" href="../../../assets/js/477.e3f203de.js"><link rel="prefetch" href="../../../assets/js/478.b34f8b1c.js"><link rel="prefetch" href="../../../assets/js/479.06d48874.js"><link rel="prefetch" href="../../../assets/js/48.0c8a5846.js"><link rel="prefetch" href="../../../assets/js/480.ffc77ad4.js"><link rel="prefetch" href="../../../assets/js/481.2475d5aa.js"><link rel="prefetch" href="../../../assets/js/482.5da370b4.js"><link rel="prefetch" href="../../../assets/js/483.2531f6c1.js"><link rel="prefetch" href="../../../assets/js/484.3f8b3ccf.js"><link rel="prefetch" href="../../../assets/js/485.fb51e0ee.js"><link rel="prefetch" href="../../../assets/js/486.881d8443.js"><link rel="prefetch" href="../../../assets/js/487.36a959cb.js"><link rel="prefetch" href="../../../assets/js/488.086e154c.js"><link rel="prefetch" href="../../../assets/js/489.2afd2906.js"><link rel="prefetch" href="../../../assets/js/49.40a46041.js"><link rel="prefetch" href="../../../assets/js/490.a517239f.js"><link rel="prefetch" href="../../../assets/js/491.c1f416dd.js"><link rel="prefetch" href="../../../assets/js/492.e36de27b.js"><link rel="prefetch" href="../../../assets/js/493.af28d057.js"><link rel="prefetch" href="../../../assets/js/494.00509c64.js"><link rel="prefetch" href="../../../assets/js/495.8ed86db6.js"><link rel="prefetch" href="../../../assets/js/496.412e9f2d.js"><link rel="prefetch" href="../../../assets/js/497.ed9f0eeb.js"><link rel="prefetch" href="../../../assets/js/498.2112afec.js"><link rel="prefetch" href="../../../assets/js/499.743df60a.js"><link rel="prefetch" href="../../../assets/js/5.6f1dbbc3.js"><link rel="prefetch" href="../../../assets/js/50.54de97a0.js"><link rel="prefetch" href="../../../assets/js/500.ab62befa.js"><link rel="prefetch" href="../../../assets/js/501.0d54ba9a.js"><link rel="prefetch" href="../../../assets/js/502.2f3bf836.js"><link rel="prefetch" href="../../../assets/js/503.c3473b0e.js"><link rel="prefetch" href="../../../assets/js/504.cb511490.js"><link rel="prefetch" href="../../../assets/js/505.a2cc8bdc.js"><link rel="prefetch" href="../../../assets/js/506.f8ea663a.js"><link rel="prefetch" href="../../../assets/js/507.9cc67e94.js"><link rel="prefetch" href="../../../assets/js/508.e08708a1.js"><link rel="prefetch" href="../../../assets/js/509.437502c3.js"><link rel="prefetch" href="../../../assets/js/51.af5d6a98.js"><link rel="prefetch" href="../../../assets/js/510.f5e8a583.js"><link rel="prefetch" href="../../../assets/js/511.6fe511a5.js"><link rel="prefetch" href="../../../assets/js/512.459d2d68.js"><link rel="prefetch" href="../../../assets/js/513.a5f4a768.js"><link rel="prefetch" href="../../../assets/js/514.5fd44c68.js"><link rel="prefetch" href="../../../assets/js/515.c046ffb4.js"><link rel="prefetch" href="../../../assets/js/516.a972f386.js"><link rel="prefetch" href="../../../assets/js/517.7c804920.js"><link rel="prefetch" href="../../../assets/js/518.ff4a2545.js"><link rel="prefetch" href="../../../assets/js/519.b1a8d590.js"><link rel="prefetch" href="../../../assets/js/52.b766b60a.js"><link rel="prefetch" href="../../../assets/js/520.2d34cada.js"><link rel="prefetch" href="../../../assets/js/521.f14247fe.js"><link rel="prefetch" href="../../../assets/js/522.38b72172.js"><link rel="prefetch" href="../../../assets/js/523.203fa4ac.js"><link rel="prefetch" href="../../../assets/js/524.236cda03.js"><link rel="prefetch" href="../../../assets/js/525.8caaad18.js"><link rel="prefetch" href="../../../assets/js/526.9a217f1e.js"><link rel="prefetch" href="../../../assets/js/527.06dc5a4f.js"><link rel="prefetch" href="../../../assets/js/528.fa884c79.js"><link rel="prefetch" href="../../../assets/js/529.d39c75ca.js"><link rel="prefetch" href="../../../assets/js/53.eae65ff7.js"><link rel="prefetch" href="../../../assets/js/530.65b18758.js"><link rel="prefetch" href="../../../assets/js/531.85af388d.js"><link rel="prefetch" href="../../../assets/js/532.9d3f253a.js"><link rel="prefetch" href="../../../assets/js/533.77c2fa0b.js"><link rel="prefetch" href="../../../assets/js/534.924b0eb2.js"><link rel="prefetch" href="../../../assets/js/535.5854a4b0.js"><link rel="prefetch" href="../../../assets/js/536.8b57b538.js"><link rel="prefetch" href="../../../assets/js/537.ccb7ba0f.js"><link rel="prefetch" href="../../../assets/js/538.b9c6c631.js"><link rel="prefetch" href="../../../assets/js/539.63164d8a.js"><link rel="prefetch" href="../../../assets/js/54.9b89b352.js"><link rel="prefetch" href="../../../assets/js/540.7e721167.js"><link rel="prefetch" href="../../../assets/js/541.0cd3f048.js"><link rel="prefetch" href="../../../assets/js/542.6c5df759.js"><link rel="prefetch" href="../../../assets/js/543.e1eebdf2.js"><link rel="prefetch" href="../../../assets/js/544.a3042061.js"><link rel="prefetch" href="../../../assets/js/545.1a468f89.js"><link rel="prefetch" href="../../../assets/js/546.892c1841.js"><link rel="prefetch" href="../../../assets/js/547.c6b5ef7e.js"><link rel="prefetch" href="../../../assets/js/548.19d884b6.js"><link rel="prefetch" href="../../../assets/js/549.7c34a773.js"><link rel="prefetch" href="../../../assets/js/55.b8a1964e.js"><link rel="prefetch" href="../../../assets/js/550.25f7d121.js"><link rel="prefetch" href="../../../assets/js/551.b9f92580.js"><link rel="prefetch" href="../../../assets/js/552.bf452147.js"><link rel="prefetch" href="../../../assets/js/553.4815ac56.js"><link rel="prefetch" href="../../../assets/js/554.c253ebb4.js"><link rel="prefetch" href="../../../assets/js/555.4dbef2bf.js"><link rel="prefetch" href="../../../assets/js/556.69f1a9f4.js"><link rel="prefetch" href="../../../assets/js/557.679e8f40.js"><link rel="prefetch" href="../../../assets/js/558.9515c7f8.js"><link rel="prefetch" href="../../../assets/js/559.eb17cff7.js"><link rel="prefetch" href="../../../assets/js/56.88fa6666.js"><link rel="prefetch" href="../../../assets/js/560.0de96a04.js"><link rel="prefetch" href="../../../assets/js/561.cdf49465.js"><link rel="prefetch" href="../../../assets/js/562.2e895986.js"><link rel="prefetch" href="../../../assets/js/563.40a31d36.js"><link rel="prefetch" href="../../../assets/js/564.08f15bf2.js"><link rel="prefetch" href="../../../assets/js/565.e7625c6a.js"><link rel="prefetch" href="../../../assets/js/566.71ee556d.js"><link rel="prefetch" href="../../../assets/js/567.7b3e4bc8.js"><link rel="prefetch" href="../../../assets/js/568.1a3c07dc.js"><link rel="prefetch" href="../../../assets/js/569.4c496338.js"><link rel="prefetch" href="../../../assets/js/57.519e80e3.js"><link rel="prefetch" href="../../../assets/js/570.d152813e.js"><link rel="prefetch" href="../../../assets/js/571.e46946bd.js"><link rel="prefetch" href="../../../assets/js/572.ae215a0d.js"><link rel="prefetch" href="../../../assets/js/573.da7b2e6c.js"><link rel="prefetch" href="../../../assets/js/574.f068c09e.js"><link rel="prefetch" href="../../../assets/js/575.f2a128db.js"><link rel="prefetch" href="../../../assets/js/576.caf5cd68.js"><link rel="prefetch" href="../../../assets/js/577.b6c04041.js"><link rel="prefetch" href="../../../assets/js/578.a7707d73.js"><link rel="prefetch" href="../../../assets/js/579.ed17d056.js"><link rel="prefetch" href="../../../assets/js/58.963c2309.js"><link rel="prefetch" href="../../../assets/js/580.88a2ce36.js"><link rel="prefetch" href="../../../assets/js/581.f99a65fa.js"><link rel="prefetch" href="../../../assets/js/582.220ce66c.js"><link rel="prefetch" href="../../../assets/js/583.5d42e8cc.js"><link rel="prefetch" href="../../../assets/js/584.0737b3ce.js"><link rel="prefetch" href="../../../assets/js/585.516b187d.js"><link rel="prefetch" href="../../../assets/js/586.420c42c4.js"><link rel="prefetch" href="../../../assets/js/587.e3deb3dd.js"><link rel="prefetch" href="../../../assets/js/588.c9bfc1e3.js"><link rel="prefetch" href="../../../assets/js/589.ee0f90f3.js"><link rel="prefetch" href="../../../assets/js/59.d9b10f83.js"><link rel="prefetch" href="../../../assets/js/590.ea859bba.js"><link rel="prefetch" href="../../../assets/js/591.4b9781fd.js"><link rel="prefetch" href="../../../assets/js/592.c9da8cab.js"><link rel="prefetch" href="../../../assets/js/6.064acd80.js"><link rel="prefetch" href="../../../assets/js/60.7e89993c.js"><link rel="prefetch" href="../../../assets/js/61.f2039f26.js"><link rel="prefetch" href="../../../assets/js/62.474d18a7.js"><link rel="prefetch" href="../../../assets/js/63.3a64d334.js"><link rel="prefetch" href="../../../assets/js/64.4a3787c2.js"><link rel="prefetch" href="../../../assets/js/65.120ee13f.js"><link rel="prefetch" href="../../../assets/js/66.e7ff207d.js"><link rel="prefetch" href="../../../assets/js/67.55a68541.js"><link rel="prefetch" href="../../../assets/js/68.33f8c7cd.js"><link rel="prefetch" href="../../../assets/js/69.69d65c56.js"><link rel="prefetch" href="../../../assets/js/7.ce2273f5.js"><link rel="prefetch" href="../../../assets/js/70.93a544a5.js"><link rel="prefetch" href="../../../assets/js/71.6eb8d306.js"><link rel="prefetch" href="../../../assets/js/72.8cbae6a9.js"><link rel="prefetch" href="../../../assets/js/73.c737f071.js"><link rel="prefetch" href="../../../assets/js/74.d0ea9fc8.js"><link rel="prefetch" href="../../../assets/js/75.2219607f.js"><link rel="prefetch" href="../../../assets/js/76.278079e2.js"><link rel="prefetch" href="../../../assets/js/77.88eadfbc.js"><link rel="prefetch" href="../../../assets/js/78.257c919e.js"><link rel="prefetch" href="../../../assets/js/79.e6f5d078.js"><link rel="prefetch" href="../../../assets/js/8.e2571ee7.js"><link rel="prefetch" href="../../../assets/js/80.40ba7856.js"><link rel="prefetch" href="../../../assets/js/81.1fab2c8f.js"><link rel="prefetch" href="../../../assets/js/82.6eaad50e.js"><link rel="prefetch" href="../../../assets/js/83.9f3e6e81.js"><link rel="prefetch" href="../../../assets/js/84.060f32c2.js"><link rel="prefetch" href="../../../assets/js/85.c069396c.js"><link rel="prefetch" href="../../../assets/js/86.ced213eb.js"><link rel="prefetch" href="../../../assets/js/87.763c4879.js"><link rel="prefetch" href="../../../assets/js/88.69fe1a6a.js"><link rel="prefetch" href="../../../assets/js/89.b87612a8.js"><link rel="prefetch" href="../../../assets/js/9.5f2fc60c.js"><link rel="prefetch" href="../../../assets/js/90.6132ccb6.js"><link rel="prefetch" href="../../../assets/js/91.27dbc3bd.js"><link rel="prefetch" href="../../../assets/js/92.88832461.js"><link rel="prefetch" href="../../../assets/js/93.0166bd69.js"><link rel="prefetch" href="../../../assets/js/94.e29b12f1.js"><link rel="prefetch" href="../../../assets/js/95.f1245827.js"><link rel="prefetch" href="../../../assets/js/96.9c45438e.js"><link rel="prefetch" href="../../../assets/js/97.e8783a06.js"><link rel="prefetch" href="../../../assets/js/98.60628afa.js"><link rel="prefetch" href="../../../assets/js/99.b3cb9d0f.js">
    <link rel="stylesheet" href="../../../assets/css/0.styles.f7de6701.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="../../../index.html" class="home-link router-link-active"><!----> <span class="site-name">代码重工</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <!----></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><!---->  <!----> </aside> <main class="page"> <div class="theme-default-content content__default"><p></p><div class="table-of-contents"><ul><li><a href="#_1、数据页">1、数据页</a></li><li><a href="#_2、数据页内部">2、数据页内部</a><ul><li><a href="#_1主键排序">①主键排序</a></li><li><a href="#_2数据页编号">②数据页编号</a></li></ul></li><li><a href="#_3、数据页如何便于查找">3、数据页如何便于查找</a><ul><li><a href="#_1排序">①排序</a></li><li><a href="#_2双向链表">②双向链表</a></li><li><a href="#_3扫描全表">③扫描全表</a></li></ul></li><li><a href="#_4、数据页设定目录">4、数据页设定目录</a><ul><li><a href="#_1第一层目录">①第一层目录</a></li><li><a href="#_2第二层目录">②第二层目录</a></li><li><a href="#_3最终形成一个树形结构">③最终形成一个树形结构</a></li></ul></li><li><a href="#_5、非主键字段创建索引">5、非主键字段创建索引</a><ul><li><a href="#_1不同数据类型的搜索方式">①不同数据类型的搜索方式</a></li><li><a href="#_2单列索引">②单列索引</a></li><li><a href="#_3多列索引">③多列索引</a></li><li><a href="#_4回表">④回表</a></li><li><a href="#_5不回表的情况">⑤不回表的情况</a></li><li><a href="#_6小结">⑥小结</a></li></ul></li><li><a href="#_6、索引的分类">6、索引的分类</a><ul><li><a href="#_1普通索引">①普通索引</a></li><li><a href="#_2唯一性索引">②唯一性索引</a></li><li><a href="#_3主键索引">③主键索引</a></li><li><a href="#_4单列索引">④单列索引</a></li><li><a href="#_5多列-组合、联合-索引">⑤多列(组合、联合)索引</a></li><li><a href="#_6全文索引">⑥全文索引</a></li><li><a href="#_7补充-空间索引">⑦补充：空间索引</a></li></ul></li></ul></div><p></p> <h1 id="第五章-索引的原理"><a href="#第五章-索引的原理" class="header-anchor">#</a> 第五章 索引的原理</h1> <h2 id="_1、数据页"><a href="#_1、数据页" class="header-anchor">#</a> 1、数据页</h2> <p>假设有一张数据库表：</p> <table><thead><tr><th>emp_id(主键)</th> <th>emp_age(非主键)</th> <th>emp_name(非主键)</th></tr></thead> <tbody><tr><td>1</td> <td>21</td> <td>tom</td></tr> <tr><td>2</td> <td>22</td> <td>jerry</td></tr> <tr><td>3</td> <td>23</td> <td>bob</td></tr> <tr><td>4</td> <td>24</td> <td>harry</td></tr> <tr><td>5</td> <td>25</td> <td>lily</td></tr> <tr><td>……</td> <td>……</td> <td>……</td></tr></tbody></table> <p>对于 InnoDB 存储引擎来说，<span style="color:blue;font-weight:bold;">最小的存储单位</span>就是：<span style="color:blue;font-weight:bold;">页</span>。那么存放原始数据的页就称为<span style="color:blue;font-weight:bold;">数据页</span>。</p> <p><img src="" alt="images"></p> <p>一个数据页默认的大小是：<span style="color:blue;font-weight:bold;">16KB</span>。如果我们假设一条记录所占空间的字节数是 1KB，那么这个数据页大致能够存储 16 条记录。那么如果这个数据库表总共有 100 万条记录，那么肯定是需要很多数据页来存放这些数据。</p> <h2 id="_2、数据页内部"><a href="#_2、数据页内部" class="header-anchor">#</a> 2、数据页内部</h2> <h3 id="_1主键排序"><a href="#_1主键排序" class="header-anchor">#</a> ①主键排序</h3> <p>数据页内部对主键进行了排序，所以当我们在一个数据页内部根据主键查找记录时，会根据<span style="color:blue;font-weight:bold;">二分法</span>进行查找。找到主键就能够很快找到这条记录。</p> <h3 id="_2数据页编号"><a href="#_2数据页编号" class="header-anchor">#</a> ②数据页编号</h3> <p>为了在众多数据页中，定位每一个数据页，数据页需要有编号。</p> <p><img src="" alt="images"></p> <h2 id="_3、数据页如何便于查找"><a href="#_3、数据页如何便于查找" class="header-anchor">#</a> 3、数据页如何便于查找</h2> <h3 id="_1排序"><a href="#_1排序" class="header-anchor">#</a> ①排序</h3> <p>根据<span style="color:blue;font-weight:bold;">主键值</span>对所有数据页进行排序。</p> <p><img src="" alt="images"></p> <h3 id="_2双向链表"><a href="#_2双向链表" class="header-anchor">#</a> ②双向链表</h3> <p>把所有数据页组成<span style="color:blue;font-weight:bold;">环状双向链表</span>。</p> <p><img src="" alt="images"></p> <p>好处是：假设搜索主键值大于 30 的记录，那么找到主键值为 30 的记录之后，后面所有的记录就都可以快速找到和返回了。</p> <h3 id="_3扫描全表"><a href="#_3扫描全表" class="header-anchor">#</a> ③扫描全表</h3> <p>就目前的数据存储方式来说，如果要找到某一个主键值所在的数据页，需要一页一页的查找，这无疑是非常缓慢的。所以肯定需要做进一步优化。</p> <h2 id="_4、数据页设定目录"><a href="#_4、数据页设定目录" class="header-anchor">#</a> 4、数据页设定目录</h2> <h3 id="_1第一层目录"><a href="#_1第一层目录" class="header-anchor">#</a> ①第一层目录</h3> <h4 id="_1-设置"><a href="#_1-设置" class="header-anchor">#</a> [1]设置</h4> <ul><li>选取每一个数据页中最小的主键值</li> <li>最小主键值 + 当前数据页的页码 = 目录页中的一条记录</li> <li><span style="color:blue;font-weight:bold;">主键值 + 页码 组成的记录</span>放在一起组成了<span style="color:blue;font-weight:bold;">目录页</span></li> <li>不管是数据页还是目录页都是页，默认大小都是 16KB</li> <li>估算目录页能够存储多少条记录？
<ul><li>主键值占 8 字节</li> <li>页码占 8 字节</li> <li>16 KB / 16 B = 1024 条</li></ul></li></ul> <p><img src="https://heavy_code_industry.gitee.io/code_heavy_industry/assets/img/img005.c2ef9596.png" alt="images"></p> <h4 id="_2-搜索"><a href="#_2-搜索" class="header-anchor">#</a> [2]搜索</h4> <p>现在我们在 4 号目录页中有如下三个区间：</p> <ul><li>区间 1：[1,11)</li> <li>区间 2：[11,21)</li> <li>区间 3：[21, ...)</li></ul> <p>情景一：搜索主键值为 11 的记录</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">select</span> emp_id<span class="token punctuation">,</span>emp_name<span class="token punctuation">,</span>emp_age <span class="token keyword">from</span> t_emp <span class="token keyword">where</span> emp_id<span class="token operator">=</span><span class="token number">11</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>11 落在了区间 2：[11,21)</li> <li>根据目录页中的 11-&gt;2 这条记录，得知想要的数据在页码为 2 的数据页</li> <li>进入页码为 2 的数据页</li> <li>在数据页内部根据主键做二分法查找</li></ul> <p>情景二：搜索主键值为 15 的记录</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">select</span> emp_id<span class="token punctuation">,</span>emp_name<span class="token punctuation">,</span>emp_age <span class="token keyword">from</span> t_emp <span class="token keyword">where</span> emp_id<span class="token operator">=</span><span class="token number">15</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>15 落在了区间 2：[11,21)</li> <li>根据目录页中的 11-&gt;2 这条记录，得知想要的数据在页码为 2 的数据页</li> <li>进入页码为 2 的数据页</li> <li>在数据页内部根据主键做二分法查找</li></ul> <h3 id="_2第二层目录"><a href="#_2第二层目录" class="header-anchor">#</a> ②第二层目录</h3> <h4 id="_1-设置-2"><a href="#_1-设置-2" class="header-anchor">#</a> [1]设置</h4> <p>为了更进一步方便查找，命中我们要找的目录页，我们可以进一步<span style="color:blue;font-weight:bold;">给目录再设置目录</span>。</p> <p><img src="https://heavy_code_industry.gitee.io/code_heavy_industry/assets/img/img006.ef60d144.png" alt="images"></p> <h4 id="_2-搜索-2"><a href="#_2-搜索-2" class="header-anchor">#</a> [2]搜索</h4> <p>用户搜索的主键值是：60。现在 13 号目录页中包含的区间：</p> <ul><li>[1,51)</li> <li>[51,150)</li> <li>[150,...)</li></ul> <p>所以 60 会落在 [51,150) 区间，所以继续查找页码为 8 的目录页：</p> <ul><li>[51,81)</li> <li>[81,100)</li> <li>[100,...)</li></ul> <p>所以 60 会落在 [51,81) 区间，所以继续查找页码为 5 的数据页，根据主键执行二分法查找。</p> <h4 id="_3-扩展"><a href="#_3-扩展" class="header-anchor">#</a> [3]扩展</h4> <p>如果有需要，就可以给目录的目录再设置目录……直到向上汇总到一个唯一的根节点。</p> <h3 id="_3最终形成一个树形结构"><a href="#_3最终形成一个树形结构" class="header-anchor">#</a> ③最终形成一个树形结构</h3> <h4 id="_1-示意图"><a href="#_1-示意图" class="header-anchor">#</a> [1]示意图</h4> <p><img src="https://heavy_code_industry.gitee.io/code_heavy_industry/assets/img/img007.51e380a7.png" alt="images"></p> <h4 id="_2-对接概念"><a href="#_2-对接概念" class="header-anchor">#</a> [2]对接概念</h4> <p><span style="color:blue;font-weight:bold;">B+Tree</span>：其实这就是 B+Tree。在我们这里例子中，数据的主键就是 B+Tree 里面的索引，页码值就是指向数据存储位置的指针。只不过指针有可能指向数据页，也有可能指向目录页。</p> <p><span style="color:blue;font-weight:bold;">B+Tree 节点</span>：目录页或数据页都算是 B+Tree 中的节点。</p> <ul><li>页：是 InnoDB 存储引擎管理数据的最小单元。</li> <li>节点：是从树形结构角度来说的。</li></ul> <p><span style="color:blue;font-weight:bold;">聚簇索引</span>：索引和原始数据存放在一起的存储形式。聚簇索引都是由主键作为索引值，所以一个表中只有一个聚簇索引。</p> <p><span style="color:blue;font-weight:bold;">非聚簇索引</span>：索引和原始数据没有存放在一起的存储形式。非聚簇索引都算由非主键字段创建的，所以可以有多个。</p> <p><span style="color:blue;font-weight:bold;">索引值</span>：在当前例子中，我们是拿主键字段的值作为索引。</p> <h4 id="_3-估算-b-tree-能够存储的记录数量"><a href="#_3-估算-b-tree-能够存储的记录数量" class="header-anchor">#</a> [3]估算 B+Tree 能够存储的记录数量</h4> <p>结论：根据下面的推算使用 B+Tree 的形式来组织数据库表中数据的存储方式，<span style="color:blue;font-weight:bold;">只需要 2~4 层</span>就足够了。</p> <ul><li>一层：只有一个根节点
<ul><li>这个根节点只能是数据页节点</li> <li>一个数据页默认大小是：16KB</li> <li>假设一条记录占空间：1K</li> <li>能够存储的是数据数量：16 条</li></ul></li> <li>两层：
<ul><li>根节点：目录页
<ul><li>主键：8 B</li> <li>页码：8 B</li> <li>目录页的每一条记录：16 B</li> <li>目录页能够存储的记录数量：16 KB / 16 B = 1024</li> <li>对目录页来说：内部存储了多少条记录，就会指向多少个子节点</li></ul></li> <li>叶子节点：数据页
<ul><li>能够存储的是数据数量：16 条</li></ul></li> <li>总和：目录页容量 × 数据页容量 = 1024 × 16 = 16384 条记录</li></ul></li> <li>三层：
<ul><li>根节点：1 个目录页</li> <li>第二层：1024 个目录页</li> <li>第三层：目录页的个数 × 目录页的容量 = 1024 × 1024 = 1048576 个数据页</li> <li>总和：数据页的个数 × 数据页的容量= 1048576 × 16 = 16,777,216 条记录</li></ul></li> <li>四层：
<ul><li>根节点：1 个目录页</li> <li>第二层：1024 个目录页</li> <li>第三层：目录页的个数 × 目录页的容量 = 1024 × 1024 = 1048576 个目录页</li> <li>第四层：目录页的个数 × 目录页的容量 = 1048576 × 1024 = 1,073,741,824 个数据页</li> <li>总和：数据页的个数 × 数据页的容量= 1,073,741,824 × 16 = 17,179,869,184 条记录</li></ul></li></ul> <h4 id="_4-b-tree-层次对性能的影响"><a href="#_4-b-tree-层次对性能的影响" class="header-anchor">#</a> [4]B+Tree 层次对性能的影响</h4> <ul><li>根节点常驻内存。</li> <li>访问下一层的节点会导致一次 I/O。</li> <li>所以层数越少，I/O 的次数就越少，性能就越好。</li></ul> <h4 id="_5-btree-为什么高瘦"><a href="#_5-btree-为什么高瘦" class="header-anchor">#</a> [5]BTree 为什么高瘦？</h4> <ul><li>BTree 的体型：<span style="color:blue;font-weight:bold;">高瘦</span> <ul><li>BTree 每个节点都存原始数据。</li> <li>每个节点的默认大小 16 KB，存了数据，能够用来存『主键 + 页码』的空间就不够了</li> <li>每个节点容纳子节点的数量就很少，导致深度增加</li> <li>对于查询来说：深度增加 1 层，I/O 次数增加一次</li> <li>所以性能方面 BTree 不如 B+Tree</li></ul></li> <li>B+Tree 的体现：<span style="color:blue;font-weight:bold;">扁平、矮胖</span></li></ul> <h4 id="_6-btree-为什么每个节点都存原始数据"><a href="#_6-btree-为什么每个节点都存原始数据" class="header-anchor">#</a> [6]BTree 为什么每个节点都存原始数据？</h4> <p><img src="" alt="images"></p> <p>BTree 将所有主键排序，对于父节点来说：</p> <ul><li>节点内部：主键是排序的</li> <li>左边子节点：存放比父节点中最小主键还要小的主键</li> <li>右边子节点：存放比父节点中最大主键还要大的主键</li> <li>中间子节点：存放在父节点中主键区间范围的主键</li></ul> <p>所以在 BTree 中，在父节点出现过的主键不会在子节点中出现。反过来说：每一个主键只在一个节点中出现。所以主键关联的数据只能在主键所在的节点中保存。</p> <h4 id="_7-btree-和-b-tree-的区别总结"><a href="#_7-btree-和-b-tree-的区别总结" class="header-anchor">#</a> [7]BTree 和 B+Tree 的区别总结</h4> <ul><li>BTree 中的节点（页），没有目录页、数据页之分。</li> <li>BTree 的节点中既存储原始数据，又存储子节点的页码值。</li> <li>在 BTree 中，每个主键值只能在一个节点中出现，所以每个节点都需要保存原始数据</li> <li>BTree 因为每个节点都保存原始数据，所以用来保存节点页码的空间变小</li> <li>BTree 每个节点能够容纳的子节点比 B+Tree 要少</li> <li>BTree 整体层数比 B+Tree 要多</li> <li>MySQL 每读取一层节点，就要做一次 I/O 操作</li> <li>I/O 操作越多，性能越差</li></ul> <blockquote><p>BTree：高瘦<br>
层数多 ---&gt; I/O 次数多 ---&gt; 性能低<br>
[1]为什么层数多？<br>
因为父节点中，能够容纳的子节点的指针少。<br>
[2]为什么能够容纳的子节点的指针少？<br>
数据页的存储空间是固定的，默认是 16KB。<br>
在这个固定的空间内，要拿出一部分存储原始数据。<br>
所以能够用来存储索引记录的空间就变少了。<br>
所以能够存储的索引记录的数量就变少了。<br>
索引记录=索引值+子节点指针<br>
[3]BTree里为什么每个节点都存数据？<br>
因为对BTree的父节点来说，内部的主键值也是经过排序的。<br>
比父节点中最小的主键值还小的主键值存放在父节点左边的子节点中。<br>
比父节点中最大的主键值还大的主键值存放在父节点右边的子节点中。<br>
最终导致：每一个主键都只在一个节点中出现过一次，所以原始数据就必须和主键保存在同一个节点中。<br>
B+Tree：矮胖<br>
层数少 ---&gt; I/O 次数少 ---&gt; 性能高</p></blockquote> <h4 id="_8-有数据表和索引表之分吗"><a href="#_8-有数据表和索引表之分吗" class="header-anchor">#</a> [8]有数据表和索引表之分吗？</h4> <p>这要看索引表具体指的是哪种类型的索引。</p> <ul><li>聚簇索引：那就没有额外的数据表，数据本身就是按照 B+Tree 的形式组成了聚簇索引——以主键作为索引值。</li> <li>非聚簇索引：非聚簇索引相对于聚簇索引来说可以称之为是另外一张表。</li></ul> <p>但其实我们应该更进一步，更精确的来说：数据库底层维护表中的数据其实并没有『表』这个概念，而全部都是以索引的形式保存的。所以说数据库底层存储数据并不是我们从逻辑上看到的『行』和『列』的形式，而是树形结构。</p> <p>结论：</p> <ul><li>数据的<span style="color:blue;font-weight:bold;">逻辑结构</span>：<span style="color:blue;font-weight:bold;">表、行、列</span></li> <li>数据的<span style="color:blue;font-weight:bold;">物理结构</span>：<span style="color:blue;font-weight:bold;">B+Tree</span></li></ul> <h2 id="_5、非主键字段创建索引"><a href="#_5、非主键字段创建索引" class="header-anchor">#</a> 5、非主键字段创建索引</h2> <div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>前面我们所论述的，都是从主键出发建立索引，方便 SQL 语句中根据主键查询数据。</p> <p>而用户在 SQL 语句中很多时候不是根据主键查询的。所以非主键字段也应该在适合的情况下创建索引，提升查询效率。</p></div> <h3 id="_1不同数据类型的搜索方式"><a href="#_1不同数据类型的搜索方式" class="header-anchor">#</a> ①不同数据类型的搜索方式</h3> <ul><li>数值类型：就拿这个数值字段本身作为索引值</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">select</span> emp_id<span class="token punctuation">,</span>emp_name<span class="token punctuation">,</span>emp_age <span class="token keyword">from</span> t_emp <span class="token keyword">where</span> emp_age<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li><p>字符串类型：</p> <ul><li>较短字符串：就拿这个字符串字段本身作为索引值</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">select</span> emp_id<span class="token punctuation">,</span>emp_name<span class="token punctuation">,</span>emp_age <span class="token keyword">from</span> t_emp <span class="token keyword">where</span> emp_name<span class="token operator">=</span><span class="token string">'tom'</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>较长字符串：通常就拿这个字符串字段开头（最左边）的一部分本身作为索引值</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">select</span> emp_id<span class="token punctuation">,</span>emp_name<span class="token punctuation">,</span>emp_age <span class="token keyword">from</span> t_emp <span class="token keyword">where</span> emp_decs <span class="token operator">like</span> <span class="token string">&quot;I come from UK%&quot;</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ul> <div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>对于目录页来说，它的结构固定就是下图所示：</p> <p><img src="" alt="images"></p> <p>那么我们用什么值做索引呢？</p> <ul><li><span style="color:blue;font-weight:bold;">聚簇索引</span>：<span style="color:blue;font-weight:bold;">主键值</span>作为<span style="color:blue;font-weight:bold;">索引</span>值</li> <li><span style="color:blue;font-weight:bold;">非聚簇索引</span>：<span style="color:blue;font-weight:bold;">主键之外其它形式数据</span>作为<span style="color:blue;font-weight:bold;">索引</span>值</li></ul></div> <h3 id="_2单列索引"><a href="#_2单列索引" class="header-anchor">#</a> ②单列索引</h3> <h4 id="_1-概念"><a href="#_1-概念" class="header-anchor">#</a> [1]概念</h4> <p>假设还是使用前面的数据库表：</p> <table><thead><tr><th>emp_id(主键)</th> <th>emp_age(非主键)</th> <th>emp_name(非主键)</th></tr></thead> <tbody><tr><td>1</td> <td>21</td> <td>tom</td></tr> <tr><td>2</td> <td>22</td> <td>jerry</td></tr> <tr><td>3</td> <td>23</td> <td>bob</td></tr> <tr><td>4</td> <td>24</td> <td>harry</td></tr> <tr><td>5</td> <td>25</td> <td>lily</td></tr> <tr><td>……</td> <td>……</td> <td>……</td></tr></tbody></table> <p>现在我们为 emp_age 字段建立索引。此时因为我们只用到了这一个字段，所以叫单列索引。</p> <h4 id="_2-存储结构"><a href="#_2-存储结构" class="header-anchor">#</a> [2]存储结构</h4> <p>和前面聚簇索引对比，也就是<span style="color:blue;font-weight:bold;">数据页</span>存储的结构不同：</p> <p><img src="https://heavy_code_industry.gitee.io/code_heavy_industry/assets/img/img011.6c8e806d.png" alt="images"></p> <p>下面咱们拿实际数据举个例子：</p> <p><img src="https://heavy_code_industry.gitee.io/code_heavy_industry/assets/img/img012.fc83e602.png" alt="images"></p> <h4 id="_3-字符串类型能排序吗"><a href="#_3-字符串类型能排序吗" class="header-anchor">#</a> [3]字符串类型能排序吗？</h4> <p>B+Tree 要求作为索引值的数据必须经过排序，这样方便执行二分法查找。而字符串可以根据它底层的 Unicode 码进行排序。因为 Unicode 码是二进制数据，二进制数据（或十六进制数据）也是整数，所以当然可以比较大小，可以排序。</p> <h3 id="_3多列索引"><a href="#_3多列索引" class="header-anchor">#</a> ③多列索引</h3> <p>也叫：组合索引、联合索引。顾名思义，这种索引就是在<span style="color:blue;font-weight:bold;">数据页</span>包含了多个字段的值。</p> <p><img src="https://heavy_code_industry.gitee.io/code_heavy_industry/assets/img/img013.24e85730.png" alt="images"></p> <h3 id="_4回表"><a href="#_4回表" class="header-anchor">#</a> ④回表</h3> <p><img src="https://heavy_code_industry.gitee.io/code_heavy_industry/assets/img/img014.b937da08.png" alt="images"></p> <p>我们根据这个以非聚簇索引的索引列大小排序的 B+ 树只能确定我们要查找记录的主键值，所以如果我们想根据非聚簇索引的索引列的值查找到完整的用户记录的话，仍然需要<span style="color:blue;font-weight:bold;">回到聚簇索引</span>中再查一遍，这个过程称为<span style="color:blue;font-weight:bold;">回表</span>。也就是根据非聚簇索引的索引列的值查询一条完整的用户记录需要使用到<span style="color:blue;font-weight:bold;">两棵B+树</span>！</p> <p>问题：为什么我们还需要一次回表操作呢？直接把完整的用户记录放到叶子节点不OK吗？</p> <p>回答：
如果把完整的用户记录放到叶子节点是可以不用回表。但是<code>太占地方</code>了，相当于每建立一棵B+树都需要把所有的用户记录再都拷贝一遍，这就有点太浪费存储空间了。</p> <p>因为这种按照<code>非主键列</code>建立的B+树需要一次回表操作才可以定位到完整的用户记录，所以这种B+树也被称为<code>二级索引</code>（英文名<code>secondary index</code>），或者<code>辅助索引</code>。如果我们使用的是 emp_age 列的大小作为 B+ 树的排序规则，那么我们也可以把这个B+树叫做：为 emp_age 列建立的索引。</p> <p>非聚簇索引的存在不影响数据在聚簇索引中的组织，所以一张表可以有多个非聚簇索引。</p> <p><img src="https://heavy_code_industry.gitee.io/code_heavy_industry/assets/img/img010.b80a6842.jpg" alt="images"></p> <h3 id="_5不回表的情况"><a href="#_5不回表的情况" class="header-anchor">#</a> ⑤不回表的情况</h3> <p>如果最终需要使用的数据，在非聚簇索引中直接就能够拿到，那就不需要回表再查询聚簇索引。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">create</span> <span class="token keyword">index</span> idx_emp_name <span class="token keyword">on</span> t_emp<span class="token punctuation">(</span>emp_name<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">select</span> emp_id<span class="token punctuation">,</span>emp_name <span class="token keyword">from</span> t_emp <span class="token keyword">where</span> emp_name <span class="token operator">like</span> <span class="token string">'tom%'</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_6小结"><a href="#_6小结" class="header-anchor">#</a> ⑥小结</h3> <p>聚簇索引与非聚簇索引的原理不同，在使用上也有一些区别：</p> <ol><li>聚簇索引的<code>叶子节点</code>存储的就是我们的数据记录，非聚簇索引的叶子节点存储的是数据位置——主键。非聚簇索引不会影响数据表的物理存储顺序。</li> <li>一个表<code>只能有一个聚簇索引</code>，因为原始数据只能有一种排序存储的方式，但可以有<code>多个非聚簇索引</code>，也就是多个索引目录提供数据检索。</li></ol> <h2 id="_6、索引的分类"><a href="#_6、索引的分类" class="header-anchor">#</a> 6、索引的分类</h2> <p>MySQL的索引包括普通索引、唯一性索引、全文索引、单列索引、多列索引和空间索引等。</p> <ul><li><p>从<code>功能逻辑</code>上说，索引主要有 4 种，分别是普通索引、唯一索引、主键索引和全文索引。</p></li> <li><p>按照<code>物理实现方式</code>，索引可以分为 2 种：聚簇索引和非聚簇索引。</p></li> <li><p>按照<code>字段个数</code>进行划分，分成单列索引和联合索引。</p></li></ul> <h3 id="_1普通索引"><a href="#_1普通索引" class="header-anchor">#</a> ①普通索引</h3> <p>在创建普通索引时，不附加任何限制条件，主要用于提高查询效率。这类索引可以创建在<code>任何数据类型</code>中，其值是否唯一和非空，要由字段本身的完整性约束条件决定。建立索引以后，可以通过索引进行查询。例如，在表<code>t_student</code>的字段<code>stuid</code>上建立一个普通索引，查询记录时就可以根据该索引进行查询。</p> <h3 id="_2唯一性索引"><a href="#_2唯一性索引" class="header-anchor">#</a> ②唯一性索引</h3> <p>使用<code>UNIQUE参数</code>可以设置索引为唯一性索引，在创建唯一性索引时，限制该索引的值必须是唯一的，但允许有空值。在一张数据表里<code>可以有多个</code>唯一索引。</p> <p>例如，在表t_student的字段name中创建唯一性索引，那么字段name的值就必须是唯一的。通过唯一性索引，可以更快速地确定某条记录。</p> <h3 id="_3主键索引"><a href="#_3主键索引" class="header-anchor">#</a> ③主键索引</h3> <p>主键索引就是一种<code>特殊的唯一性索引</code>，在唯一索引的基础上增加了不为空的约束，也就是 NOT NULL+UNIQUE，一张表里<code>最多只有一个</code>主键索引。</p> <p><code>Why？</code> 这是由主键索引的物理实现方式决定的，因为数据存储在文件中只能按照一种顺序进行存储。</p> <h3 id="_4单列索引"><a href="#_4单列索引" class="header-anchor">#</a> ④单列索引</h3> <p>在表中的单个字段上创建索引。单列索引只根据该字段进行索引。单列索引可以是普通索引，也可以是唯一性索引，还可以是全文索引。只要保证该索引只对应一个字段即可。一个表可以<code>有多个</code>单列索引。</p> <h3 id="_5多列-组合、联合-索引"><a href="#_5多列-组合、联合-索引" class="header-anchor">#</a> ⑤多列(组合、联合)索引</h3> <p>多列索引是在表的多个字段组合上创建一个索引。该索引指向创建时对应的多个字段，可以通过这几个字段进行查询，但是只有查询条件中使用了这些字段中的第一个字段时才会被使用。例如，在表中的字段id、name和gender上建立一个多列索引<code>idx_id_name_gender</code>，只有在查询条件中使用了字段id时该索引才会被使用。使用组合索引时遵循<code>最左前缀集合</code>。</p> <h3 id="_6全文索引"><a href="#_6全文索引" class="header-anchor">#</a> ⑥全文索引</h3> <p>全文索引（也称全文检索）是目前<code>搜索引擎</code>使用的一种关键技术。它能够利用【<code>分词技术</code>】等多种算法智能分析出文本文字中关键词的频率和重要性，然后按照一定的算法规则智能地筛选出我们想要的搜索结果。</p> <p>使用参数<code>FULLTEXT</code>可以设置索引为全文索引。在定义索引的列上支持值的全文查找，允许在这些索引列中插入重复值和空值。全文索引只能创建在<code>CHAR</code>、<code>VARCHAR</code>或<code>TEXT</code>类型及其系列类型的字段上，**查询数据量较大的字符串类型的字段时，使用全文索引可以提高查询速度。**例如，表<code>t_student</code>的字段<code>information</code>是<code>TEXT</code>类型，该字段包含了很多文字信息。在字段information上建立全文索引后，可以提高查询字段information的速度。</p> <h4 id="_1-自然语言的全文索引"><a href="#_1-自然语言的全文索引" class="header-anchor">#</a> [1]自然语言的全文索引</h4> <p>默认情况下，或者使用 in natural language mode 修饰符时，match() 函数对文本集合执行自然语言搜索。</p> <p>自然语言搜索引擎将计算每一个文档对象和查询的相关度。这里，相关度是基于匹配的关键词的个数，以及关键词在文档中出现的次数。**在整个索引中出现次数越少的词语，匹配时的相关度就越高。**相反，非常常见的单词将不会被搜索，如果一个词语的在超过 50% 的记录中都出现了，那么自然语言的搜索将不会搜索这类词语。</p> <p>这个机制也比较好理解，比如说，一个数据表存储的是一篇篇的文章，文章中的常见词、语气词等等，出现的肯定比较多，搜索这些词语就没什么意义了，需要搜索的是那些文章中有特殊意义的词，这样才能把文章区分开。</p> <h4 id="_2-布尔全文索引"><a href="#_2-布尔全文索引" class="header-anchor">#</a> [2]布尔全文索引</h4> <p>在布尔搜索中，我们可以在查询中自定义某个被搜索的词语的相关性，当编写一个布尔搜索查询时，可以通过一些前缀修饰符来定制搜索。</p> <p>MySQL 内置的修饰符，上面查询最小搜索长度时，搜索结果 ft_boolean_syntax 变量的值就是内置的修饰符，下面简单解释几个，更多修饰符的作用可以查手册。</p> <p><strong>限制：</strong>
MySQL数据库从3.23.23版开始支持全文索引，但MySQL5.6.4以前<code>只有Myisam支持</code>，5.6.4版本以后<code>innodb才支持</code>，但是官方版本不支持<code>中文分词</code>，需要第三方分词插件。在5.7.6版本，MySQL内置了<code>ngram全文解析器</code>，用来支持亚洲语种的分词。测试或使用全文索引时，要先看一下自己的 MySQL 版本、存储引擎和数据类型是否支持全文索引。</p> <p>随着大数据时代的到来，关系型数据库应对全文索引的需求已力不从心，逐渐被 <code>solr</code>、<code>elasticSearch</code>等专门的搜索引擎所替代。</p> <h3 id="_7补充-空间索引"><a href="#_7补充-空间索引" class="header-anchor">#</a> ⑦补充：空间索引</h3> <p>使用<code>参数SPATIAL</code>可以设置索引为<code>空间索引</code>。空间索引只能建立在空间数据类型上，这样可以提高系统获取空间数据的效率。MySQL中的空间数据类型包括<code>GEOMETRY</code>、<code>POINT</code>、<code>LINESTRING</code>和<code>POLYGON</code>等。目前只有MyISAM存储引擎支持空间检索，而且索引的字段不能为空值。对于初学者来说，这类索引很少会用到。</p> <p><strong>小结：不同的存储引擎支持的索引类型也不一样</strong>
**InnoDB ：**支持 B-tree、Full-text 等索引，不支持 Hash 索引；
<strong>MyISAM ：</strong> 支持 B-tree、Full-text 等索引，不支持 Hash 索引；
**Memory  ：**支持 B-tree、Hash 等索引，不支持 Full-text 索引；
**NDB  ：**支持 Hash 索引，不支持 B-tree、Full-text 等索引；
**Archive  ：**不支持 B-tree、Hash、Full-text 等索引；</p> <p><a href="../chapter04/index.html">上一章</a> <a href="../index.html">回目录</a> <a href="../chapter06/index.html">下一章</a></p></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"></div></div>
    <script src="../../../assets/js/app.972bd0f0.js" defer></script><script src="../../../assets/js/4.904e76b5.js" defer></script><script src="../../../assets/js/18.9167733d.js" defer></script>
  </body>

<!-- Mirrored from heavy_code_industry.gitee.io/code_heavy_industry/pro018-MySQL-high/lecture/chapter05/index.html by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 10 May 2022 05:13:08 GMT -->
</html>
